package com.carey.gae.data;

import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

public class OAuthMemcache {
	private static final OAuthMemcache mm = new OAuthMemcache();
	private Cache cache;

	private OAuthMemcache() {
		try {
			CacheFactory cacheFactory = CacheManager.getInstance()
					.getCacheFactory();
			cache = cacheFactory.createCache(Collections.emptyMap());
		} catch (CacheException e) {
			// ...
		}
	}

	public static OAuthMemcache getInstance() {
		return mm;
	}

	@SuppressWarnings("unchecked")
	public void setProperty(String consumer_key, OAuthProperties value) {
		this.cache.put(consumer_key, value);
	}

	public OAuthProperties getProperty(String consumer_key) {
		if (containsKey(consumer_key)) {
			return (OAuthProperties) this.cache.get(consumer_key);
		} else {
			return null;
		}
	}

	public boolean containsKey(String consumer_key) {
		return this.cache.containsKey(consumer_key);
	}
}
